import 'dart:io';

import 'package:dio/dio.dart';

/**
 * 异常处理
 */
class ExceptionHandle {
  static const int success = 200;
  static const int success_not_content = 204;
  static const int unauthorized = 401;
  static const int forbidden = 403;
  static const int not_found = 404;
  static const int user_or_password_error= 426;

  static const int net_error = 1000;
  static const int parse_error = 1001;
  static const int socket_error = 1002;
  static const int http_error = 1003;
  static const int timeout_error = 1004;
  static const int unknown_error = 9999;

  static Error handleException(dynamic error){
    print(error);
    if (error is DioError){
      if (error.type == DioErrorType.other || error.type == DioErrorType.response){
      // if (error.type == DioErrorType.DEFAULT || error.type == DioErrorType.RESPONSE){
      //   print('handleException-->error=${error.toString()}');
      //   print('handleException-->message=${error.message}');
      //   print('handleException-->error=${error.error}');
      //   print('handleException-->response=${error.response is Map}');
      //   print('handleException-->response=${error.response?.data is Map}');
      //   print('handleException-->response data=${error.response?.data}');
      //   print('handleException-->type=${error.type}');
        dynamic responseDataTemp = error.response?.data;
        if((responseDataTemp!=null)&&(responseDataTemp is Map)){
          return Error(user_or_password_error, responseDataTemp['msg']);
        }
        dynamic e = error.error;
        if (e is SocketException){
          return Error(socket_error, "网络异常，请检查你的网络！");
        }
        if (e is HttpException){
          return Error(http_error, "服务器异常！");
        }
        return Error(net_error, "网络异常，请检查你的网络！");
      // }else if (error.type == DioErrorType.CONNECT_TIMEOUT ||
      //     error.type == DioErrorType.SEND_TIMEOUT ||
      //     error.type == DioErrorType.RECEIVE_TIMEOUT){
      }else if (error.type == DioErrorType.connectTimeout ||
          error.type == DioErrorType.sendTimeout ||
          error.type == DioErrorType.receiveTimeout){
        return Error(timeout_error, "连接超时！");
      }else{
//        return Error(unknown_error, "未知异常");
        return Error(unknown_error, "系统繁忙，请稍候重试");
      }
    }else {
//      return Error(unknown_error, "未知异常");
      return Error(unknown_error, "系统繁忙，请稍候重试");
    }
  }
}

class Error{
  int status;
  String message;

  Error(this.status, this.message);
}